package com.lxm.controller;


import com.lxm.entity.*;
import com.lxm.service.IAdminRoleService;
import com.lxm.service.IRoleResourceService;
import com.lxm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lxm
 * @since 2021-11-18
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IAdminRoleService irs;
    @Autowired
    private IRoleService rs;
    @Autowired
    private IRoleResourceService rrs;
    //给用户赋角色
    @PostMapping("/grantRidForUserId")
    public ResultEntity grantRidForUserId(@RequestBody AdminRole adminRole){
        if(adminRole==null){
            return ResultEntity.error("赋角色失败");
        }
        System.out.println(adminRole);
        List<Integer> roleIds = adminRole.getRoleIds();

        AdminRole ar=new AdminRole();
        if(roleIds==null){
            return ResultEntity.error("赋角色失败");
        }
        ar.setUserId(adminRole.getUserId());
        for(int i=0;i<roleIds.size();i++){
            ar.setRoleId(roleIds.get(i));
           irs.save(ar);
        }
        return ResultEntity.ok();
    }
    //角色添加
    @PostMapping("/add")
    public ResultEntity addRole(@RequestBody Role role){
        boolean i = rs.save(role);
        if(i){
            return ResultEntity.ok();
        }
        return ResultEntity.error("角色添加失败");
    }

    //删除角色
    @DeleteMapping("/delete/{roleId}")
    public ResultEntity deleteRole(@PathVariable int roleId){
        boolean i = rs.removeById(roleId);if(i){
            return ResultEntity.ok();
        }
        return ResultEntity.error("角色删除失败");

    }
    //根据角色标记查询角色信息
    @GetMapping("/selectOne/{roleId}")
    public ResultEntity<Role> selectOne(@PathVariable int roleId){
        Role role = rs.getById(roleId);
        if(role==null){
            return ResultEntity.error("角色不存在");
        }
        return ResultEntity.ok(role);
    }
    //给角色赋权
    @PostMapping("/grantResource")
    public ResultEntity grantResource(@RequestBody RoleResourceDTO r){
        Integer roleId = r.getRoleId();
        List<Integer> resourceIds = r.getResourceIds();
        if(resourceIds==null){
            return ResultEntity.error();
        }
        RoleResource rr=new RoleResource();
        rr.setRoleId(roleId);
        for(int i=0;i<resourceIds.size();i++){
            rr.setResourceId(resourceIds.get(i));
            rrs.save(rr);
        }
        return ResultEntity.ok();
    }

    //获取某个角色的权限ID
    @GetMapping("/selectResourcesByRoleId/{roleId}")
    public ResultEntity<List<Integer>> selectResourcesByRoleId(@PathVariable int roleId){
        List<RoleResource> roleResources = rrs.selectResourceByRoleId(roleId);
        List<Integer> resourceIds = roleResources.stream().map(roleResource -> roleResource.getResourceId()).collect(Collectors.toList());
        if(resourceIds==null){
            return ResultEntity.error();
        }
        return ResultEntity.ok(resourceIds);
    }

}

